package feedbackServlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.helpSession.HelpRequestsessionRemote;

public class InsertFeedback extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

	private void drawPage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String commento;
		Context jndiCtx;
		int valutazione, idReq;
		idReq = Integer.parseInt(request.getParameter("idReq"));
		commento = request.getParameter("commento");
		valutazione = Integer.parseInt(request.getParameter("valutazione"));
		try {
			jndiCtx = new InitialContext();
			HelpRequestsessionRemote hrs = (HelpRequestsessionRemote) jndiCtx
					.lookup("Progetto/HelpRequestsession/remote");
			if (hrs.insertFeedback(idReq, valutazione, commento))
				request.getSession().setAttribute("done", "insert");
			else
				request.getSession().setAttribute("done", "error");
		} catch (Exception e) {
			request.getSession().setAttribute("done", "error");
		}
		response.sendRedirect("/ProgettoWeb/ShowSingleHelpServlet.html?idReq="+idReq);
	}
}
